
{{include file="../tools/jquery_validator2.htm"}}
<div class="userbox">
  <h2>创建小店</h2>
  <form name="category" id="category" method="post">

  <div class="left usersdianz">
    <table>
      <tr>
        <td colspan="2" class="nameorange">确定店长</td>
      </tr>
      <tr>
        <td colspan="2" class="colororange">(系统自动提取小店名称）马上就要开业了！店员都到齐了么？</td>
      </tr>
      <tr>
        <td>店&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;长：</td>
        <td width="440" class="ts5"><input type="text" name="mag_name" value="{{$mag.user_name}}" readonly="readonly"/></td>
      </tr>
      {{if $cat.memberss}}
      {{foreach from=$cat.memberss item=list key=ey}}
      <tr class="clone " >
        <td >店员：</td>
        <td ><input type="text" name="member[]"  class="member" value="{{$list.user_name}}" avater='{{$list.avater}}' uname='{{$list.name}}'  user_id='{{$list.user_id}}' checked="false"  disabled="disabled"/>
           <div class="glinfo"><input type="checkbox" name="mag[]"  value="1" class="mag" {{if $list.mag==1}}checked="checked"{{/if}}/>
           <span class="guanliq">授予本店管理权</span> 
           <small onclick="cloneTr()" class="green none">&#43;增加</small>  
           </div>
          
           <div class="error_bar"></div>
        </td>
      </tr>
      {{/foreach}}
       {{/if}}
     
       <tr class="clone " >
        <td>店&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;员：</td>
        <td ><input type="text" name="member[]" onblur="checkUser($(this))" class="member"/>
           <div class="glinfo"><input type="checkbox" name="mag[]"  value="1" class="mag"/>
           <span class="guanliq">授予本店管理权</span> 
           <small onclick="cloneTr()" class="green">&#43;增加</small>  
           </div>
           <em class="clear">请填写店员注册时的用户名</em>
        </td>
      </tr>
      
     
      
      <tr>
        <td>小店地址：</td>
        <td><input type="text" name="address" value="{{$cat.address}}"/></td>
      </tr>
      <tr>
        <td>联系电话：</td>
        <td><input type="text" name="phone" value="{{$cat.phone}}"/></td>
      </tr>
       <tr>
        <td>店铺口号：</td>
        <td><textarea name="title" style="height:100px">{{$cat.title}}</textarea></td>
      </tr>
      <tr>
        <td colspan="2" class="colororange">其他人还没有注册？ 没关系，今后还可以由管理员进行完善</td>
      </tr>
    </table>
    <div class="icobtn">
      <input name="user_act" value="steps_b" type="hidden" />
      <input name="members" value="{{$cat.members}}" type="hidden"/>
      <input name="update" value="{{if $cat.phone}}1{{else}}0{{/if}}" type="hidden"/>
      <input class="prev" type="button" onclick="location.href='main.php?act=user&user_act=steps_a&c_id={{$c_id}}'"/>
      <input class="next" type="button" onclick="sendData()"/>
    </div>
  </div>
 
  </form>
  {{include file="./user_act_right.htm"}} 
  <!--right end--> 
  
</div>
<script>
$(function() {
    $('#category').validate({

        rules: {
            address: {
                //minlength: 2,
                //required: true

            },
            phone: {
                minlength: 2,
                required: true

            }
			,
            title: {
                maxlength: 120
            }
        },

        messages: {
            address: {
                required: "请填写小店地址"

            },
            phone: {
                required: "请填写联系电话"

            },
			title: {
                maxlength: "备注信息不能超过120个字符!"
            }
        }
		,
		success: function(element) {
				element
				.text('OK!').addClass('valid')
				.closest('.control-group').removeClass('error').addClass('success');
  		}
    });

})

function checkUser(obj) {
    var member = $(".member");
    member.attr({
        "check": ""
    });
    obj.attr({
        "check": "true"
    });
    var val = obj.val();
    if (val) {
        var data = "act=is_register&user_name=" + val;
        Main.AJAX(sep + "/ajax.php", data, checkUserRes);
    }
	else
	{
		var td = $("input[check=true]").parent("td");	
		  td.find(".error_bar").remove();
	}
}
function checkUserRes(res) {
    var res =  eval("("+res+")");
	
    var td = $("input[check=true]").parent("td");
    if (res == "") {
        var msg = Main.errorMsg('该用户未注册!');
		var err_bar = td.find(".error_bar");
		err_bar.remove();
		var html = ' <div class="error_bar">'+msg+'</div>';
        td.append(html);
    }
	else if(res.cat_id*1)
	{
		var msg = Main.errorMsg('该用户已经加入其它店铺!');
		var err_bar = td.find(".error_bar");
		err_bar.remove();
		var html = ' <div class="error_bar">'+msg+'</div>';
        td.append(html);
	}
	else {
        $("input[check=true]").attr({
            "user_id": res.user_id,
			'avater':res.avater,
			'uname':res.info.name
        });
        td.find(".error_bar").remove();
		
    }
}

function sendData() {
   var members = getMembers();
    var error = $(".errors").html() || false;
	
    if ($('#category').valid() && !error) {
		 
		$("input[name=members]").val($.toJSON(members));
       $('#category').submit();
    }
}

function getMembers() {
    var data = new Object;
    var trs = $(".clone");
    for (var i = 0; i < trs.length; i++) {
        var arr = new Object();
        var ele = trs.eq(i);
        var member = ele.find(".member").val();
		
		
        if (member) {
			
            arr.user_name = member;
            var chk = ele.find(".mag");
			
            arr.mag = chk.is(':checked') ? '1' : '0';
			arr.user_id = ele.find(".member").attr("user_id");
			arr.avater = ele.find(".member").attr("avater");
			arr.name = ele.find(".member").attr("uname");
			data[i] = arr
        }
       

    }
	return data;
}

function cloneTr() {
    var trs = $(".clone");
    var tr = trs.last();
    var new_tr = tr.clone();
    new_tr.find("input").attr({
        "check": "",
        "checked": false
    }).val("");
    new_tr.find(".error_bar").html("");
    tr.after(new_tr);
    
}
  </script>